use ffmpeg_sys_next::{
    AVChannelLayout, AVSampleFormat, SwrContext, swr_alloc, swr_alloc_set_opts2, swr_convert,
    swr_free, swr_init,
};
use std::ffi::{c_int, c_void};

pub fn swr_alloc_safe() -> *mut SwrContext {
    unsafe { swr_alloc() }
}

pub fn swr_alloc_set_opts2_safe(
    ps: *mut *mut SwrContext,
    out_ch_layout: *const AVChannelLayout,
    out_sample_fmt: AVSampleFormat,
    out_sample_rate: c_int,
    in_ch_layout: *const AVChannelLayout,
    in_sample_fmt: AVSampleFormat,
    in_sample_rate: c_int,
    log_offset: c_int,
    log_ctx: *mut c_void,
) -> c_int {
    unsafe {
        swr_alloc_set_opts2(
            ps,
            out_ch_layout,
            out_sample_fmt,
            out_sample_rate,
            in_ch_layout,
            in_sample_fmt,
            in_sample_rate,
            log_offset,
            log_ctx,
        )
    }
}

pub fn swr_convert_safe(
    s: *mut SwrContext,
    out: *mut *mut u8,
    out_count: c_int,
    in_: *mut *const u8,
    in_count: c_int,
) -> c_int {
    unsafe { swr_convert(s, out, out_count, in_, in_count) }
}

pub fn swr_init_safe(s: *mut SwrContext) -> c_int {
    unsafe { swr_init(s) }
}

pub fn swr_free_safe(s: *mut *mut SwrContext) {
    unsafe { swr_free(s) }
}
